package com.example.musicserver.controller;

import com.example.musicserver.config.ResponseBodyMessage;
import com.example.musicserver.constant.Constants;
import com.example.musicserver.model.Music;
import com.example.musicserver.model.User;
import com.example.musicserver.service.LoveMusicService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jdk.jfr.Frequency;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-20
 * Time: 15:08
 */
@RestController
@RequestMapping("/loveMusic")
@Slf4j
public class LoveMusicController {
    @Resource
    private LoveMusicService loveMusicService;

    /**
     * 收藏音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id , HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        log.info("喜欢的音乐的id是{}",musicId);
        HttpSession httpSession = request.getSession(false);//没有session不创建
        if(httpSession == null ||
                httpSession.getAttribute(Constants.USERINFO_SESSION_KEY) == null){
            log.info("没有登陆");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user = (User)httpSession.getAttribute(Constants.USERINFO_SESSION_KEY);//获取用户信息
        int userId = user.getId();
        log.info("当前登录用户的id是{}",userId);
        //查询当前⽤⼾是否点赞过该⾳乐
        Music music = loveMusicService.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if(music != null){
            return new ResponseBodyMessage<>(-1,"该用户已经收藏过该音乐了",false);
        }
        boolean result = loveMusicService.insertLoveMusic(userId,musicId);
        if(result){
            return new ResponseBodyMessage<>(0,"该音乐收藏成功",true);
        }
        return new ResponseBodyMessage<>(-1,"该音乐收藏失败",false);
    }

    /**
     * 查找收藏的音乐
     * @param musicName
     * @param request
     * @return
     */

    @RequestMapping("/findLoveMusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName
            , HttpServletRequest request){
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null
                || httpSession.getAttribute(Constants.USERINFO_SESSION_KEY)== null){
            log.info("没有登陆");
            return new ResponseBodyMessage<>(-1,"没有登录",null);
        }

        User user = (User)httpSession.getAttribute(Constants.USERINFO_SESSION_KEY);
        int userId = user.getId();
        List<Music> musics = new ArrayList<>();
        if(musicName == null || musicName == ""){
            musics = loveMusicService.findAllLoveMusicByUserId(userId);
        }else{
            musics = loveMusicService.findLoveMusicByKeyAndUserId(musicName, userId);//歌曲名不为null就模糊查询
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的收藏音乐",musics);
    }

    @RequestMapping("/deleteLoveMusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id , HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null
                || httpSession.getAttribute(Constants.USERINFO_SESSION_KEY)== null){
            log.info("没有登陆");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user = (User)httpSession.getAttribute(Constants.USERINFO_SESSION_KEY);
        int userId = user.getId();
        int ret = loveMusicService.deleteLoveMusic(userId,musicId);
        if(ret == 1){
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }else{
            return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
        }
    }


















}
